<template>
  <div class="setting-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card class="box-card">
        <!-- 使用 Tabs 组件完成标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first" class="tab-pane">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="adddialog"
                >新增角色</el-button
              >
            </el-row>
            <!-- 使用 Table 组件实现用户角色的渲染 -->
            <el-table :data="rolesList" border style="width: 100%">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                    size="small"
                    type="success"
                    @click="setRoles(scope.row)"
                    >分配权限</el-button
                  >
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRoles(scope.row)"
                    >编辑</el-button
                  >
                  <el-button
                    size="small"
                    type="danger"
                    @click="delRoles(scope.row)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页区域 -->
            <el-pagination
              :current-page="query.page"
              :page-sizes="[5, 10, 15, 20, 25]"
              :page-size="query.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息" class="tab-pane">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form
              label-width="120px"
              style="margin-top: 50px"
              :model="companyformData"
            >
              <el-form-item label="公司名称">
                <el-input
                  disabled
                  style="width: 400px"
                  v-model="companyformData.name"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  disabled
                  style="width: 400px"
                  v-model="companyformData.companyAddress"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  disabled
                  style="width: 400px"
                  v-model="companyformData.mailbox"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                  v-model="companyformData.remarks"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>

        <!-- 新增弹框 -->
        <el-dialog
          title="新增角色"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          :visible.sync="showDialog"
        >
          <el-form
            ref="roleForm"
            :model="roleForm"
            :rules="roleRules"
            label-width="100px"
          >
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="roleForm.name" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="roleForm.description" />
            </el-form-item>
          </el-form>

          <!-- 底部 -->
          <el-row slot="footer" type="flex" justify="center">
            <el-col :span="6" class="cols">
              <el-button class="buttons" size="small" @click="cancleRoles"
                >取消</el-button
              >
              <el-button
                class="buttons"
                size="small"
                type="primary"
                @click="roleSubmit"
                >确定</el-button
              >
            </el-col>
          </el-row>
        </el-dialog>
      </el-card>

      <el-dialog 
      title="分配权限"
       :visible.sync="dialogVisible"
       @close="setVisible" 
       >
        <AssignPermission
          :permIds="permIds"
          @close="close"
          @enter="enter"
          :permissinonlist="permissinonlist"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
// import {getRoleIdAPI} from '../../api/setting'
import AssignPermission from "./assignPermission.vue";
import { getPermissionListAPI } from "../../api/permission";
import { transTree } from "../../utils/index";
import {
  getRoleIdAPI,
  getRolesAPI,
  getCompanyInfoAPI,
  addRoleAPI,
  updateRoleAPI,
  deleteRoleAPI,
  assignPermAPI
} from "../../api/setting";
export default {
  components: { AssignPermission },
  data() {
    return {
      activeName: "first",
      query: {
        page: 1, // 当前页面
        pagesize: 10, // 页面显示的条数
      },
      rolesList: [], // 角色列表
      total: 0, // 角色数据总条数
      companyId: 1, //公司ID
      companyformData: {}, //公司对象
      showDialog: false, // 控制弹框的隐藏和展示
      // 添加角色
      roleForm: {
        name: "",
        description: "",
      },
      // 添加角色验证
      roleRules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
        description: [
          { required: true, message: "角色描述不能为空", trigger: "blur" },
        ],
      },
      isEdit: false, // 是否处于编辑状态
      dialogVisible: false, //点击分配--》权限点弹窗打开
      permissinonlist: [], //所有权限数据
      permIds: [], //此角色现有的权限点数据
      okid:'',//分配权限时，点击的内行ID
    };
  },
  created() {
    this.getRolesAPIFn();
    this.getCompanyInfoAPIFn();
    this.getPermissionListAPIfn();
  },
  methods: {
    //页面数据请求
    async getRolesAPIFn() {
      const res = await getRolesAPI(this.query);
      // console.log(res);
      this.rolesList = res.data.data.rows;
      this.total = res.data.data.total;
    },
    //页面公司信息
    async getCompanyInfoAPIFn() {
      const res = await getCompanyInfoAPI(this.companyId);
      // console.log(res);
      this.companyformData = res.data.data;
    },
    //点击新增按钮时打开dialog
    adddialog() {
      (this.roleForm = {
        name: "",
        description: "",
      }),
        (this.showDialog = true);
      this.isEdit = false;
    },
    // 角色弹框-> 确定按钮
    roleSubmit() {
      this.$refs.roleForm.validate(async (valid) => {
        if (valid) {
          //编辑为ture
          if (this.isEdit) {
            const res = await updateRoleAPI(this.roleForm);
          } else {
            const res = await addRoleAPI(this.roleForm);
            //  console.log(res);
            this.$message({
              message: "添加成功消息",
              type: "success",
            });
          }

          this.getRolesAPIFn();
          this.showDialog = false;
        }
      });
    },

    // 角色弹框-> 取消按钮
    cancleRoles() {
      this.showDialog = false;
    },
    // 每页显示的条数发生改变时触发
    handleSizeChange(size) {
      this.query.pagesize = size;
    },

    // 当前页面发生改变时触发
    handleCurrentChange(page) {
      this.query.page = page, 
      this.getRolesAPIFn();
    },

    // 设置角色 分配权限
    async setRoles(obj) {
      // console.log(11);
      this.dialogVisible = true;
       this.okid=obj.id
      const res = await getRoleIdAPI(obj.id);
      this.permIds = res.data.data.permIds;
      //  console.log(res);
      //   console.log(this.permIds);
     
    },
    // 点击子组件确定按钮
  async enter(lists) {
    //  console.log('陈佳伟');
     console.log(lists);
        const res=await assignPermAPI({
          id: this.okid,
          permIds:lists
        })
        // console.log(res);
         this.getRolesAPIFn();
         this.dialogVisible = false;
    },

   
 


    //点击子组件取消按钮
    close() {
      this.dialogVisible = false;
    },
    //获取权限列表
    async getPermissionListAPIfn() {
      const res = await getPermissionListAPI();
      // console.log(res);
      this.permissinonlist = transTree(res.data.data, "0");
      // console.log( this.permissinonlist);
    },
   //弹窗关闭事件
   setVisible(){
     this.permIds=[]

   },
    //...................................................................

    // 编辑角色
    async editRoles(roloobj) {
      this.isEdit = true;
      this.showDialog = true;
      const res = await getRoleIdAPI(roloobj.id);
      //  console.log(res);
      this.roleForm = res.data.data;
    },

    // 删除角色
    delRoles(roloobj) {
      this.$confirm("此操作将永久删除该角色, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then((res) => {
        // console.log(res);
        deleteRoleAPI(roloobj.id).then((res) => {
          // console.log(res);
          if (this.rolesList.length == 1) {
            this.query.page--;
            if (this.query.page == 0) {
              this.query.page = 1;
            }
          }
          this.getRolesAPIFn();
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px;
}

.el-pagination {
  text-align: center;
  margin-top: 20px;
}
.buttons {
  margin: 0 10px;
}
.cols {
  display: flex;
}
</style>